package lium.lbs.lbsim.fragments

import android.content.Intent
import android.support.v4.app.FragmentTransaction
import android.util.TypedValue
import android.view.View
import android.widget.TextView
import kotlinx.android.synthetic.main.fragment_contacts.*
import lium.lbs.lbsim.R
import lium.lbs.lbsim.activity.AddFriendsGroupActivity
import lium.lbs.lbsim.activity.CreateGroupSetGroupNameActivity
import lium.lbs.lbsim.activity.MainActivity
import lium.lbs.lbsim.activity.QRCodeActivity
import lium.lbs.lbsim.utils.ContactsMenuPopWindow

class ContactsFragment: BaseFragment() {
    override fun getLayoutId(): Int { return R.layout.fragment_contacts}
    companion object {

        fun newInstance(): ContactsFragment {
            return ContactsFragment()
        }
    }

    override fun initView() {

        addOnClickListeners(R.id.choose_contacts_friend,R.id.choose_contacts_group,R.id.contacts_menu)
        chooseFragment(0)
    }

    override fun onClick(v: View?) {
        when(v!!.id){
            R.id.choose_contacts_friend -> {
                choose_contacts_friend.setTextSize(TypedValue.COMPLEX_UNIT_SP,24f)
                choose_contacts_group.setTextSize(TypedValue.COMPLEX_UNIT_SP,18f)
                chooseFragment(0)
            }
            R.id.choose_contacts_group -> {
                choose_contacts_friend.setTextSize(TypedValue.COMPLEX_UNIT_SP,18f)
                choose_contacts_group.setTextSize(TypedValue.COMPLEX_UNIT_SP,24f)
                chooseFragment(1)
            }
            R.id.contacts_menu -> {
                showpop()
                popmenu!!.showPopupWindow(contacts_menu)
            }
        }
    }

    var popmenu:ContactsMenuPopWindow?=null
    private fun showpop(){
        if (popmenu==null){
            popmenu = ContactsMenuPopWindow(activity as MainActivity)
            popmenu!!.contentView.findViewById<TextView>(R.id.menu_creategroup).setOnClickListener {
                val intent = Intent(activity, CreateGroupSetGroupNameActivity::class.java)
                startActivity(intent)
                popmenu!!.dismiss()
            }
            popmenu!!.contentView.findViewById<TextView>(R.id.menu_add).setOnClickListener {
                val intent = Intent(activity, AddFriendsGroupActivity::class.java)
                startActivity(intent)
                popmenu!!.dismiss()
            }
            popmenu!!.contentView.findViewById<TextView>(R.id.menu_scancode).setOnClickListener {
                val intent = Intent(activity, QRCodeActivity::class.java)
                startActivity(intent)
                popmenu!!.dismiss()
            }
        }
    }

    private fun chooseFragment(postion :Int) {
        var fragmentTransaction: FragmentTransaction = childFragmentManager.beginTransaction()
        if (postion==0){
           var fragment = FriendListFragment.newInstance()
            fragmentTransaction.replace(R.id.contacts_panel,fragment,fragment.javaClass.name) .addToBackStack(null)

        }else if (postion==1){
            var fragment = GroupListFragment.newInstance()
            fragmentTransaction.replace(R.id.contacts_panel,fragment,fragment.javaClass.name) .addToBackStack(null)
        }
        fragmentTransaction.commit()
    }

}